package javaNIO1.channel;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;

/**
 * 通道到通道
 * @author Guojt
 * @date 2024/7/17
 */
public class cToc {

     // 注意 文件通道总是阻塞的
    public static void main(String[] args) throws IOException {

        if(args.length == 0){
            System.out.println("请输入参数");
            return;
        }
        catFiles(Channels.newChannel(System.out),args);

    }

    private static void catFiles(WritableByteChannel target, String[] files) throws IOException {
        for (int i = 0; i < files.length; i++) {
            FileInputStream fileInputStream = new FileInputStream(files[i]);

            FileChannel channel = fileInputStream.getChannel();
            try{
                //  向文件中写入系统输出通道中的数据
                channel.transferTo(0,channel.size(),target);
            }catch (IOException e){
                e.printStackTrace();
            }finally {
                channel.close();
                fileInputStream.close();
            }
        }

    }
}
